/////////////////////////////////////////////////////////////////////////////
// Name:        ax_shortcutinputdialog.cpp
// Purpose:     
// Author:      Julian Smart
// Modified by: 
// Created:     13/04/2009 22:28:43
// RCS-ID:      
// Copyright:   
// Licence:     
/////////////////////////////////////////////////////////////////////////////

#include "wx/wx.h"

////@begin includes
////@end includes

#include "ax/ax_shortcutinputdialog.h"
#include "ax/ax_speech.h"

////@begin XPM images
////@end XPM images


/*!
 * AxShortcutInputDialog type definition
 */

IMPLEMENT_DYNAMIC_CLASS( AxShortcutInputDialog, wxDialog )


/*!
 * AxShortcutInputDialog event table definition
 */

BEGIN_EVENT_TABLE( AxShortcutInputDialog, wxDialog )

    EVT_TIMER(wxID_ANY, AxShortcutInputDialog::OnTimer)

////@begin AxShortcutInputDialog event table entries
    EVT_INIT_DIALOG( AxShortcutInputDialog::OnInitDialog )

    EVT_TEXT( wxID_ANY, AxShortcutInputDialog::OnAnyTextUpdated )

    EVT_BUTTON( wxID_OK, AxShortcutInputDialog::OnOkClick )

////@end AxShortcutInputDialog event table entries

END_EVENT_TABLE()


/*!
 * AxShortcutInputDialog constructors
 */

AxShortcutInputDialog::AxShortcutInputDialog()
{
    Init();
}

AxShortcutInputDialog::AxShortcutInputDialog( wxWindow* parent,
        const wxString& message, const wxString& caption, const wxShortcutItem& defaultValue,
        const wxPoint& pos, const wxSize& size, long style)
{
    Init();
    Create(parent, message, caption, defaultValue, pos, size, style);
}


/*!
 * AxTextInputDialog creator
 */

bool AxShortcutInputDialog::Create( wxWindow* parent,
        const wxString& message, const wxString& caption, const wxShortcutItem& defaultValue,
        const wxPoint& pos, const wxSize& size, long style)
{
    m_disableEvents = true;

    SetExtraStyle(wxWS_EX_BLOCK_EVENTS);
    wxDialog::Create( parent, wxID_ANY, caption, pos, size, style );

    CreateControls(defaultValue, message);
    if (GetSizer())
    {
        GetSizer()->SetSizeHints(this);
    }
    Centre();

    m_disableEvents = false;
    return true;
}


/*!
 * AxShortcutInputDialog destructor
 */

AxShortcutInputDialog::~AxShortcutInputDialog()
{
    m_timer.Stop();
    
////@begin AxShortcutInputDialog destruction
////@end AxShortcutInputDialog destruction
}


/*!
 * Member initialisation
 */

void AxShortcutInputDialog::Init()
{
    m_shortcutManager = NULL;
    m_speech = NULL;
    m_disableEvents = false;
    m_lastSpoke = 0;
    
    m_timer.SetOwner(this);
    m_timer.Start(1000);

////@begin AxShortcutInputDialog member initialisation
    m_descriptionCtrl = NULL;
    m_valueCtrl = NULL;
    m_detailsCtrl = NULL;
////@end AxShortcutInputDialog member initialisation
}


/*!
 * Control creation for AxTextInputDialog
 */

void AxShortcutInputDialog::CreateControls(const wxShortcutItem& defaultValue, const wxString& message)
{    
////@begin AxShortcutInputDialog content construction
    AxShortcutInputDialog* itemDialog1 = this;

    wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
    itemDialog1->SetSizer(itemBoxSizer2);

    wxBoxSizer* itemBoxSizer3 = new wxBoxSizer(wxVERTICAL);
    itemBoxSizer2->Add(itemBoxSizer3, 1, wxGROW|wxALL, 5);

    m_descriptionCtrl = new wxStaticText( itemDialog1, wxID_STATIC, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
    m_descriptionCtrl->Wrap(250);
    itemBoxSizer3->Add(m_descriptionCtrl, 0, wxALIGN_LEFT|wxALL, 5);

    m_valueCtrl = new wxShortcutCtrl( itemDialog1, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(280, -1), 0 );
    itemBoxSizer3->Add(m_valueCtrl, 0, wxGROW|wxALL, 5);

    m_detailsCtrl = new wxTextCtrl( itemDialog1, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(-1, 100), wxTE_MULTILINE|wxTE_READONLY );
    itemBoxSizer3->Add(m_detailsCtrl, 0, wxGROW|wxALL, 5);

    wxStdDialogButtonSizer* itemStdDialogButtonSizer7 = new wxStdDialogButtonSizer;

    itemBoxSizer3->Add(itemStdDialogButtonSizer7, 0, wxGROW|wxALL, 5);
    wxButton* itemButton8 = new wxButton( itemDialog1, wxID_OK, _("OK"), wxDefaultPosition, wxDefaultSize, 0 );
    itemButton8->SetDefault();
    itemStdDialogButtonSizer7->AddButton(itemButton8);

    wxButton* itemButton9 = new wxButton( itemDialog1, wxID_CANCEL, _("Cancel"), wxDefaultPosition, wxDefaultSize, 0 );
    itemStdDialogButtonSizer7->AddButton(itemButton9);

    itemStdDialogButtonSizer7->Realize();

////@end AxShortcutInputDialog content construction

    m_descriptionCtrl->SetLabel(message);
    m_descriptionCtrl->Wrap(250);

    m_shortcut = defaultValue;
    m_valueCtrl->SetShortcut(m_shortcut);
    m_valueCtrl->ApplyShortcut();
    m_valueCtrl->SetFocus();
}


/*!
 * Should we show tooltips?
 */

bool AxShortcutInputDialog::ShowToolTips()
{
    return true;
}

/*!
 * Get bitmap resources
 */

wxBitmap AxShortcutInputDialog::GetBitmapResource( const wxString& name )
{
    // Bitmap retrieval
////@begin AxShortcutInputDialog bitmap retrieval
    wxUnusedVar(name);
    return wxNullBitmap;
////@end AxShortcutInputDialog bitmap retrieval
}

/*!
 * Get icon resources
 */

wxIcon AxShortcutInputDialog::GetIconResource( const wxString& name )
{
    // Icon retrieval
////@begin AxShortcutInputDialog icon retrieval
    wxUnusedVar(name);
    return wxNullIcon;
////@end AxShortcutInputDialog icon retrieval
}


/*!
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for wxID_OK
 */

void AxShortcutInputDialog::OnOkClick( wxCommandEvent& WXUNUSED(event) )
{
    m_shortcut = m_valueCtrl->GetShortcut();
    EndModal(wxID_OK);
}


/*!
 * wxEVT_COMMAND_TEXT_UPDATED event handler for wxID_ANY
 */

void AxShortcutInputDialog::OnAnyTextUpdated( wxCommandEvent& event )
{
    if (m_disableEvents)
        return;
    if (event.GetId() != m_valueCtrl->GetId())
        return;

    SpeakCurrentShortcut();
}

void AxShortcutInputDialog::SpeakCurrentShortcut(bool initialView)
{
    wxString value = m_valueCtrl->GetValue();
    wxShortcutItem currentItem = m_valueCtrl->GetShortcut();

    wxString clashingValues;
    if (m_shortcutManager)
    {
        wxShortcutItemArray arr(m_shortcutManager->GetMatchingShortcuts(currentItem, m_shortcut.GetId()));
        if (arr.GetCount() > 0)
        {
            clashingValues << _("The following shortcuts clash:\n\n");
            size_t i;
            for (i = 0; i < arr.GetCount(); i++)
            {
                wxShortcutItem& item = arr[i];
                clashingValues << item.GetName() << wxT(".\n");
            }
            clashingValues << _("\nPlease choose another shortcut.\n");
        }
    }

    m_detailsCtrl->SetValue(clashingValues);

    if (m_speech)
    {
        AxSpeechEvent speechEvent;
        speechEvent.SetSpeechEventType(AxSpeechEventText);
        speechEvent.SetVerbosityLevel(2);

        if (initialView)
        {
            wxString label = m_descriptionCtrl->GetLabel();
            label.Replace(wxT("\n"), wxT(" "));

            //wxLogDebug(label);

            speechEvent.SetText(label);
            m_speech->EmitSpeech(speechEvent);
        }

        speechEvent.SetText(value + wxT(".\n"));
        m_speech->EmitSpeech(speechEvent);

        if (!clashingValues.IsEmpty())
        {
            speechEvent.SetText(clashingValues);
            m_speech->EmitSpeech(speechEvent);
        }
        
        m_lastSpoke = wxGetLocalTime();
    }
}


/*!
 * wxEVT_INIT_DIALOG event handler for wxID_ANY
 */

void AxShortcutInputDialog::OnInitDialog( wxInitDialogEvent& WXUNUSED(event) )
{
    SpeakCurrentShortcut(true /* initial view */);
}

void AxShortcutInputDialog::OnTimer(wxTimerEvent& WXUNUSED(event))
{
    if (m_speech && m_speech->GetTextToSpeech().IsSpeaking())
        m_lastSpoke = wxGetLocalTime();

    int seconds = wxGetLocalTime() - m_lastSpoke;
    // If it's at least 10 seconds since we last spoke, speak again.
    if (seconds >= 8)
    {
        SpeakCurrentShortcut();
    }
}
